﻿using System.ComponentModel;
using System.Windows;

namespace Oxage.Windows.Controls
{
	public class ConsoleViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public ConsoleViewModel()
		{
			this.HasOutput = false;
		}
		#endregion

		#region Public properties
		/// <summary>
		/// Gets or sets a value indicating whether output has any content to display.
		/// </summary>
		public bool HasOutput
		{
			get
			{
				return this.hasOutput;
			}
			set
			{
				if (this.hasOutput != value)
				{
					this.hasOutput = value;
					OnPropertyChanged("HasOutput");
					OnPropertyChanged("OutputVisibility");
				}
			}
		}
		private bool hasOutput;

		/// <summary>
		/// Gets a output window part visibility.
		/// </summary>
		public Visibility OutputVisibility
		{
			get
			{
				return this.HasOutput ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		/// <summary>
		/// Gets or sets the prompt string.
		/// </summary>
		public string Prompt
		{
			get
			{
				return this.prompt;
			}
			set
			{
				if (this.prompt != value)
				{
					this.prompt = value;
					OnPropertyChanged("Prompt");
				}
			}
		}
		private string prompt;

		/// <summary>
		/// Gets or sets a value indicating whether input command should automatically appear in output.
		/// </summary>
		public bool EnableEcho
		{
			get
			{
				return this.enableEcho;
			}
			set
			{
				if (this.enableEcho != value)
				{
					this.enableEcho = value;
					OnPropertyChanged("EnableEcho");
				}
			}
		}
		private bool enableEcho;
		#endregion

		#region Public methods
		/// <summary>
		/// Splits line if exceeds the specified length (e.g. 80 chars per row)
		/// </summary>
		/// <param name="content"></param>
		/// <param name="length">Number of characters per row</param>
		/// <returns></returns>
		public string Split(string content, int length)
		{
			//if (string.IsNullOrEmpty(content))
			//{
			//  output.AppendText(Environment.NewLine);
			//  return;
			//}

			//Split 80 chars per row
			//string[] lines = content.Split('\n'); //TODO: Regex split \n, \r or \n\r
			//foreach (string line in lines)
			//{
			//  int count = (int)(line.Length / 80) + (line.Length % 80 == 0 ? 0 : 1);
			//  for (int i = 0; i < count; i++)
			//  {
			//    output.AppendText();
			//  }
			//  output.AppendText(Environment.NewLine);
			//}

			//if (waitFirstWriteLine)
			//{
			//  waitFirstWriteLine = false;
			//  output.AppendText(Environment.NewLine);
			//}

			return content;
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
